<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 全局统计组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Stat extends Cola
{
    /**
     * 用户对象
     *
     * @access protected
     * @var Widget_User
     */
    protected $user;

    /**
     * 数据库对象
     *
     * @access protected
     * @var Db
     */
    protected $db;

    /**
     * 构造函数,初始化组件
     *
     * @access public
     * @param mixed $request request对象
     * @param mixed $response response对象
     * @param mixed $params 参数列表
     * @return void
     */
    public function __construct($request, $response, $params = NULL)
    {
        parent::__construct($request, $response, $params);

        /** 初始化数据库 */
        $this->db = Db::instance();

        /** 初始化常用组件 */
        $this->user = $this->widget('Widget_User');
		
		/** 插件接口 */
		//HOOK PLUG_Stat($this);
    }

    /**
     * 获取所有的文章数目
     *
     * @access protected
     * @return integer
     */
    protected function ___contentsNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.contents')
            ->fetchObject()
			->num;
    }
    /**
     * 获取当前用户已发布的文章数目
     *
     * @access protected
     * @return integer
     */
    protected function ___currentPostsNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.contents')
		    ->where('table.contents.type = ?', 'post')
		    ->where('table.contents.uid = ?', $this->request->filter('int')->uid)
            ->fetchObject()
			->num;
    }

    /**
     * 获取当前用户已发布的文章数目
     *
     * @access protected
     * @return integer
     */
    protected function ___myPostsNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.contents')
		    ->where('table.contents.type = ?', 'post')
		    ->where('table.contents.uid = ?', $this->user->uid)
            ->fetchObject()
		    ->num;
    }


    /**
     * 获取所有的附件数目
     *
     * @access protected
     * @return integer
     */
    protected function ___attachsNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.attachs')
            ->fetchObject()
			->num;
    }
	
    /**
     * 获取所有的用户数目
     *
     * @access protected
     * @return integer
     */
    protected function ___usersNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.users')
            ->fetchObject()
			->num;
    }
	
    /**
     * 获取所有的评论数目
     *
     * @access protected
     * @return integer
     */
    protected function ___commentsNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.comments')
            ->fetchObject()
			->num;
    }
	
    /**
     * 获取当前用户显示的评论数目
     *
     * @access protected
     * @return integer
     */
    protected function ___myCommentsNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.comments')
		    ->where('table.comments.uid = ?', $this->user->uid)
            ->fetchObject()
			->num;
    }
	
    /**
     * 获取所有的分类数目
     *
     * @access protected
     * @return integer
     */
    protected function ___categoriesNum()
    {
        return $this->db->select(array('COUNT(*)' => 'num'))
		    ->from('table.metas')
            ->where('table.metas.type = ?', 'category')
            ->fetchObject()
			->num;
    }
}
